<template>

  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" icon="search" @click="handleAdd">添加APP版本</el-button>
      </div>
      <el-table :data="tableData" border class="table" style="margin-top: 5px;">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="versionName" label="APP名称"></el-table-column>
        <el-table-column prop="versionCode" label="版本"></el-table-column>
        <el-table-column prop="isForce" label="强制更新" :formatter="formatterForce"></el-table-column>
        <el-table-column prop="updateContent" label="更新内容"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <!--<el-button v-if="!scope.row.systemRole" type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            <el-button v-else icon="el-icon-s-operation" type="text"  style="color: #dfe1e7">无权操作</el-button>-->
            <el-button type="text" style="color: #f56c6c;" @click="deleteApp(scope.row.id,scope.row.versionName)">
              <i class="el-icon-delete"></i>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </div>
    <!--添加-->
    <el-dialog title="App上传" :visible.sync="addAppVisible" width="30%">
      <el-form ref="formApp" :model="formApp" label-width="70px">
        <el-form-item label="版本名称">
          <el-input v-model="formApp.versionName"></el-input>
        </el-form-item>
        <el-form-item label="版本号">
          <el-input oninput="value=value.replace(/[^\d]/g,'')" v-model="formApp.versionCode"></el-input>
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input type="textarea" v-model="formApp.updateContent"></el-input>
        </el-form-item>
        <el-form-item label="强制更新">
          <el-radio v-model="formApp.isForce" label="1">是</el-radio>
          <el-radio v-model="formApp.isForce" label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="formApp.description"></el-input>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item label="APP上传">
          <el-upload
            :limit="1"
            accept=".apk"
            class="upload-demo"
            :headers="upload.headers"
            :action="upload.url"
            :file-list="fileList"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
                <el-button @click="addAppVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  import {delVersion, listVersion, addVersion} from "@/api/system/version";
  import {getToken} from "@/utils/auth";

  export default {
    name: 'appManager',
    data: function () {
      return {
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: {Authorization: "Bearer " + getToken()},
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload"
        },
        tableData: [],
        total: 0,
        current: 1,
        pageSize: 10,
        addAppVisible: false,
        imageUrl: '',
        fileList: [],
        formApp: {
          versionName: '',
          versionCode: '',
          downloadUrl: '',
          description: '',
          isForce: '',
          updateContent: '',
          type: '',
          apkSize: 0
        }

      }
    },
    components: {},
    methods: {
      formatterForce(row, column) {
        if (row.isForce === 0) {
          return '否';
        } else if (row.isForce === 1) {
          return '是';
        }
      },

      handleCurrentChange(val) {
        this.current = val;
        this.getData();
      },
      saveAdd() {

        addVersion(this.formApp).then(response => {
          if (response.code === 200) {
            this.msgSuccess("上传APP成功");
            this.addAppVisible = false
            this.getData()
          }else {
            this.addAppVisible = false
            this.$message.warning(response.msg)
          }
        }).catch(err => {
          this.$message.error(err.toString())
        });



      },
      deleteApp(id, versionName) {
        this.$confirm(`确定要删除${versionName}？`, '提示').then(() => {
          return delVersion(id);
        }).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getData()
          } else {
            this.$message.warning("删除失败")
          }
        })
      },
      handleAdd() {
        this.addAppVisible = true
      },
      handleAvatarSuccess(res, file) {
        console.log("res:", res)
        console.log("file:", file)
        if (file.status === 'success') {
          this.formApp.apkSize = file.size;
          this.formApp.downloadUrl = file.response.fileName
        }
      },
      getData() {
        const params = {
          pageNum: this.current,
          pageSize: this.pageSize,
        }
        listVersion(params).then(res => {
          console.log(JSON.stringify(res))
          if (res.code === 200) {
            this.tableData = res.rows
            this.total = res.total
          } else {
            this.$message.warning(res.msg)
          }
        }).catch(err => {
          this.$message.error(err.toString())
        })
      }
    },
    created() {
      this.getData();
    }
  }
</script>
<style scoped>
  .table {
    background-color: white;
    margin: 10px;
    padding: 10px;
  }
</style>
